"""Lead note model for CRM-SaaS system."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from sqlalchemy import ForeignKey, Text, JSON
from sqlalchemy.orm import Mapped, mapped_column, relationship

from ..db.base_class import Base
from ..models.base import TimestampMixin

if TYPE_CHECKING:
    from ..models.lead import Lead
    from ..models.user import User


class LeadNote(Base, TimestampMixin):
    """Lead communication notes."""

    __tablename__ = "leadnote"
    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    summary: Mapped[str] = mapped_column(Text, nullable=False, comment="Communication summary")
    expectation: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="Customer expectations"
    )
    attachments: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)

    # Foreign keys
    lead_id: Mapped[int] = mapped_column(ForeignKey("leads.id"), nullable=False, index=True)
    author_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True)

    # Relationships
    lead: Mapped[Lead] = relationship("Lead", back_populates="lead_notes")
    author: Mapped[User] = relationship("User")
